package com.xuerhua.yxt.controller.sys.kccs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.reqVo.KccsCourseQueryVO;
import com.xuerhua.yxt.service.IKccsCourseInfoService;
import com.xuerhua.yxt.utils.ExcelExportUtil;
import com.xuerhua.yxt.utils.ExportSetInfoUtil;

@RequestMapping("/sys/kccsCourse/signed")
@Controller
public class SysKccsSignedController extends BaseController {
	
	@Autowired
	private IKccsCourseInfoService kccsCourseInfoService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/kccsCourse/signed";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(KccsCourseQueryVO vo){
		try {
			if(vo.getTermId() == null) {
				vo.setTermId(getCurrentTerm().getId());
			}
			vo.setCourseType(1);
			return kccsCourseInfoService.selectSignListByPage(vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("del")
	@ResponseBody Object del(String delIds){
		try {
			kccsCourseInfoService.delStudentSignByIds(Arrays.asList(delIds.split(",")));
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("courseList")
	@ResponseBody Object courseList(KccsCourseQueryVO vo){
		try {
			vo.setCourseType(1);
			return kccsCourseInfoService.selectList(vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("export")
	ModelAndView workSignExport(KccsCourseQueryVO vo) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			if(vo.getTermId() == null) {
				vo.setTermId(getCurrentTerm().getId());
			}
			vo.setRows(Integer.MAX_VALUE - 1);
			vo.setCourseType(1);
			Grid grid = kccsCourseInfoService.selectSignListByPage(vo);
			//数据源
			LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
			String title="选修课学生报名表";
			map.put(title, grid.getRows());
			
			//标题  和  标题变量
		    List<String[]> headNames = new ArrayList<String[]>();
		    headNames.add(new String[] { "年级","班号","学生姓名","证件类型","证件号","性别","课程名称","上课时间","上课教师"});
		    List<String[]> fieldNames = new ArrayList<String[]>();
		    fieldNames.add(new String[] { "gradeName","clazz","studentName","studentIdNoType","studentIdNo","studentSex","courseName","classDate","classTeacher"});
		    
		    //excel生成帮助类
		    List<Integer[]> columnFormat=new ArrayList<Integer[]>();
		    Integer [] format=new Integer[22];
		    columnFormat.add(format);
		    
		    String [] titles=new String[] {title};
		    //excel生成帮助类
		    ExportSetInfoUtil setInfo = new ExportSetInfoUtil(map,titles,headNames,fieldNames,columnFormat);
		    request.setAttribute("exportDatas", setInfo);
		    
		    //生成
			modelAndView=new ModelAndView(new ExcelExportUtil(title+".xls",""));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	
	
}
